using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using NSMBe4;
using System.IO;

namespace MKDS_Course_Editor
{
    public partial class nsbtx_ : Form
    {
        public string file_ = "";
        public bool nsbtp_ = false;
        public int timer = 0;
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(nsbtx_));
        public nsbtx_(string file, bool nsbtp)
        {
            file_ = file;
            nsbtp_ = nsbtp;
            InitializeComponent();
        }

        private void nsbtx__Load(object sender, EventArgs e)
        {
            if (nsbtp_ == true)
            {
                toolStripButton3.Enabled = true;
                toolStripButton6.Enabled = true;
                toolStripButton4.Enabled = true;
                toolStripButton8.Enabled = true;
            }
            NSMBe4.DSFileSystem.ExternalFilesystemSource f = new NSMBe4.DSFileSystem.ExternalFilesystemSource(file_);
            NSMBe4.DSFileSystem.Filesystem fs = new NSMBe4.DSFileSystem.Filesystem(f);
            NSMBe4.NSBMD.NSBTX nsbtx = new NSMBe4.NSBMD.NSBTX(new NSMBe4.DSFileSystem.File(fs, null,Path.GetFileName(file_)));
            for (int i = 0; i < nsbtx.textures.Length; i++)
            {
                listBox1.Items.Add(nsbtx.textures[i].name);
            }
            for (int i = 0; i < nsbtx.palettes.Length; i++)
            {
                listBox2.Items.Add(nsbtx.palettes[i].name);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex == -1)
            {
                listBox2.SelectedIndex = 0;
            }
            NSMBe4.DSFileSystem.ExternalFilesystemSource f = new NSMBe4.DSFileSystem.ExternalFilesystemSource(file_);
            NSMBe4.DSFileSystem.Filesystem fs = new NSMBe4.DSFileSystem.Filesystem(f);
            NSMBe4.NSBMD.NSBTX nsbtx = new NSMBe4.NSBMD.NSBTX(new NSMBe4.DSFileSystem.File(fs, null, Path.GetFileName(file_)));
            pictureBox1.Image = nsbtx.textures[listBox1.SelectedIndex].render(nsbtx.pal[listBox2.SelectedIndex]);
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                listBox1.SelectedIndex = 0;
            }
            NSMBe4.DSFileSystem.ExternalFilesystemSource f = new NSMBe4.DSFileSystem.ExternalFilesystemSource(file_);
            NSMBe4.DSFileSystem.Filesystem fs = new NSMBe4.DSFileSystem.Filesystem(f);
            NSMBe4.NSBMD.NSBTX nsbtx = new NSMBe4.NSBMD.NSBTX(new NSMBe4.DSFileSystem.File(fs, null, Path.GetFileName(file_)));
            pictureBox1.Image = nsbtx.textures[listBox1.SelectedIndex].render(nsbtx.pal[listBox2.SelectedIndex]);
        }

        private void nsbtx__Shown(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {
                pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap b = new Bitmap(openFileDialog1.FileName);
                if (openFileDialog1.FilterIndex == 2)
                {
                    b = new Bitmap(new Icon(openFileDialog1.FileName).ToBitmap());
                }
                NSMBe4.DSFileSystem.ExternalFilesystemSource f = new NSMBe4.DSFileSystem.ExternalFilesystemSource(file_);
                NSMBe4.DSFileSystem.Filesystem fs = new NSMBe4.DSFileSystem.Filesystem(f);
                NSMBe4.NSBMD.NSBTX nsbtx = new NSMBe4.NSBMD.NSBTX(new NSMBe4.DSFileSystem.File(fs, null, Path.GetFileName(file_)));

                if (b.Size.Width < pictureBox1.Image.Width || b.Size.Height < pictureBox1.Image.Height)
                {
                    MessageBox.Show("The image have to be the same or bigger size then the origional");
                    return;
                }
                nsbtx.textures[listBox1.SelectedIndex].replaceImgAndPal(b, nsbtx.pal[listBox2.SelectedIndex]);
                nsbtx.textures[listBox1.SelectedIndex].save();
                nsbtx.pal[listBox2.SelectedIndex].save();
                if (nsbtx.textures[listBox1.SelectedIndex].format == 5)
                {
                    nsbtx.str.seek((int)nsbtx.textures[listBox1.SelectedIndex].offset - nsbtx.startoffset);
                    nsbtx.str.write(nsbtx.textures[listBox1.SelectedIndex].getRawData());
                    nsbtx.str.seek((int)nsbtx.textures[listBox1.SelectedIndex].offset5 - nsbtx.startoffset);
                    nsbtx.str.write(nsbtx.textures[listBox1.SelectedIndex].getRawData5());
                    nsbtx.str.seek((int)nsbtx.palettes[listBox2.SelectedIndex].offs - nsbtx.startoffset);
                    nsbtx.str.write(nsbtx.pal[listBox2.SelectedIndex].getRawData());

                }
                else
                {
                    nsbtx.str.seek((int)nsbtx.textures[listBox1.SelectedIndex].offset - nsbtx.startoffset);
                    nsbtx.str.write(nsbtx.textures[listBox1.SelectedIndex].getRawData());
                    nsbtx.str.seek((int)nsbtx.palettes[listBox2.SelectedIndex].offs - nsbtx.startoffset);
                    nsbtx.str.write(nsbtx.pal[listBox2.SelectedIndex].getRawData());
                }
                File.WriteAllBytes(file_, nsbtx.str.getData());
                //File.WriteAllBytes(file_, nsbtx.str.getData());
                /*if (nsbtx.textures[listBox1.SelectedIndex])
                {
                    
                }*/
                //nsbtx.textures[listBox1.SelectedIndex].replace(b, 0, 0, nsbtx.palettes[listBox2.SelectedIndex], calcPalOffset(), (int)nsbtx.textures[listBox1.SelectedIndex].palSize);
                b.Dispose();
                pictureBox1.Image = nsbtx.textures[listBox1.SelectedIndex].render(nsbtx.pal[listBox2.SelectedIndex]);
                //nsbtx.close();
                //File.WriteAllBytes(file_, nsbtx.save());
            }
        }
       // int calcPalOffset()
        //{
        //    NSMBe4.NSBMD.NSBTX nsbtx = new NSMBe4.NSBMD.NSBTX(file_);
        //    return (int)(listBox2.SelectedIndex * nsbtx.textures[listBox1.SelectedIndex].palSize);
       // }

        private void timer1_Tick(object sender, EventArgs e)
        {
            NSBMD.NSBTP t = null;
            if(file_.EndsWith("nsbmd"))
            {
                t = new NSBMD.NSBTP(file_.Replace("nsbmd", "nsbtp"));
            }
            else
            {
                t = new NSBMD.NSBTP(file_.Replace("nsbtx", "nsbtp"));
            }
            NSMBe4.DSFileSystem.ExternalFilesystemSource f = new NSMBe4.DSFileSystem.ExternalFilesystemSource(file_);
            NSMBe4.DSFileSystem.Filesystem fs = new NSMBe4.DSFileSystem.Filesystem(f);
            NSMBe4.NSBMD.NSBTX nsbtx = new NSMBe4.NSBMD.NSBTX(new NSMBe4.DSFileSystem.File(fs, null, Path.GetFileName(file_)));
            pictureBox1.Image = nsbtx.textures[listBox1.Items.IndexOf(t.texname[timer])].render(nsbtx.pal[listBox2.Items.IndexOf(t.palname[timer])]);
            if (timer != t.texcount-1)
            {
                timer += 1;
            }
            else
            {
                timer = 0;
            }
        }

        private void toolStripButton3_CheckStateChanged(object sender, EventArgs e)
        {
            if (toolStripButton3.CheckState == CheckState.Checked)
            {
                toolStripButton3.Image = Properties.Resources.control_pause;
                timer1.Start();
            }
            else
            {
                toolStripButton3.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton3.Image")));
                timer1.Stop();
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (timer > 0)
            {
                timer -= 1;
                NSBMD.NSBTP t = null;
                if (file_.EndsWith("nsbmd"))
                {
                    t = new NSBMD.NSBTP(file_.Replace("nsbmd", "nsbtp"));
                }
                else
                {
                    t = new NSBMD.NSBTP(file_.Replace("nsbtx", "nsbtp"));
                }
                NSMBe4.DSFileSystem.ExternalFilesystemSource f = new NSMBe4.DSFileSystem.ExternalFilesystemSource(file_);
                NSMBe4.DSFileSystem.Filesystem fs = new NSMBe4.DSFileSystem.Filesystem(f);
                NSMBe4.NSBMD.NSBTX nsbtx = new NSMBe4.NSBMD.NSBTX(new NSMBe4.DSFileSystem.File(fs, null, Path.GetFileName(file_)));
                pictureBox1.Image = nsbtx.textures[listBox1.Items.IndexOf(t.texname[timer])].render(nsbtx.pal[listBox2.Items.IndexOf(t.palname[timer])]);
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            NSBMD.NSBTP t = null;
            if (file_.EndsWith("nsbmd"))
            {
                t = new NSBMD.NSBTP(file_.Replace("nsbmd", "nsbtp"));
            }
            else
            {
                t = new NSBMD.NSBTP(file_.Replace("nsbtx", "nsbtp"));
            }
            NSMBe4.DSFileSystem.ExternalFilesystemSource f = new NSMBe4.DSFileSystem.ExternalFilesystemSource(file_);
            NSMBe4.DSFileSystem.Filesystem fs = new NSMBe4.DSFileSystem.Filesystem(f);
            NSMBe4.NSBMD.NSBTX nsbtx = new NSMBe4.NSBMD.NSBTX(new NSMBe4.DSFileSystem.File(fs, null, Path.GetFileName(file_)));
            if (timer < t.texcount-1)
            {
                timer += 1;
                pictureBox1.Image = nsbtx.textures[listBox1.Items.IndexOf(t.texname[timer])].render(nsbtx.pal[listBox2.Items.IndexOf(t.palname[timer])]);
            }
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            if (saveFileDialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK
    && saveFileDialog2.FileName.Length > 0)
            {


                NSBMD.NSBTP t = null;
                if (file_.EndsWith("nsbmd"))
                {
                    t = new NSBMD.NSBTP(file_.Replace("nsbmd", "nsbtp"));
                }
                else
                {
                    t = new NSBMD.NSBTP(file_.Replace("nsbtx", "nsbtp"));
                }
                NSMBe4.DSFileSystem.ExternalFilesystemSource f = new NSMBe4.DSFileSystem.ExternalFilesystemSource(file_);
                NSMBe4.DSFileSystem.Filesystem fs = new NSMBe4.DSFileSystem.Filesystem(f);
                NSMBe4.NSBMD.NSBTX nsbtx = new NSMBe4.NSBMD.NSBTX(new NSMBe4.DSFileSystem.File(fs, null, Path.GetFileName(file_)));
                List<Image> b = new List<Image>();
                for (int i = 0; i < t.texcount; i++)
                {
                    b.Add(nsbtx.textures[listBox1.Items.IndexOf(t.texname[i])].render(nsbtx.pal[listBox2.Items.IndexOf(t.palname[i])]));
                }
                Tinke.Convertir.CrearGif(saveFileDialog2.FileName, b.ToArray(), 10, 0);
            }
            
        }
        
    }
}
